package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xmy.cultivate.entity.DormRoom;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xmy.cultivate.entity.DormRoomMember;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 宿舍 Mapper 接口
 * </p>
 *
 * @author hpiggy
 * @since 2025-03-02
 */
public interface DormRoomMapper extends BaseMapper<DormRoom> {


    @Select({"<script>",
            "SELECT",
            "d.*,",
            "s.`name` AS staff_name",
            "FROM",
            "dorm_room AS d",
            "LEFT JOIN staff AS s ON s.id = d.manager_id",
            "WHERE",
            "1=1",
            "<if test ='areaId != null and areaId !=&apos;&apos;'>",
            "AND d.area_id = ${areaId} ",
            "</if>",
            "<if test ='sexType != null and sexType !=&apos;&apos;'>",
            "AND d.sex_type = ${sexType}",
            "</if>",
            "<if test ='status != null and status !=&apos;&apos;'>",
            "AND d.`status` = ${status}",
            "</if>",
            "<if test ='name != null and name !=&apos;&apos;'>",
            "AND (d.room_num LIKE CONCAT('%', #{name}, '%') OR s.`name` LIKE CONCAT('%', #{name}, '%'))",
            "</if>",

            "<if test ='dueStatus != null and dueStatus !=&apos;&apos;'>",

            "<if test ='dueStatus == 0'>",
//            "AND d.pact_end_date_time &gt;  #{currentDate}",
            "AND d.pact_end_date_time &gt; DATE_ADD(#{currentDate}, INTERVAL 3 MONTH)",
            "</if>",

            "<if test ='dueStatus == 1 '>",
            "AND d.pact_end_date_time &lt;=  DATE_ADD(#{currentDate}, INTERVAL 3 MONTH) AND d.pact_end_date_time &gt;= #{currentDate}",
            "</if>",

            "<if test ='dueStatus == 2'>",
            "AND d.pact_end_date_time &lt;  #{currentDate} ",
            "</if>",

            "</if>",
            "ORDER BY d.room_num ",
            "</script>"})
    public IPage<DormRoom> getDormRoomList(IPage<DormRoom> page, @Param("areaId") String areaId , @Param("sexType") String sexType , @Param("status") String status,@Param("name") String name,@Param("dueStatus") String dueStatus,@Param("currentDate") LocalDate currentDate );


    @Select({"<script>",
            "SELECT",
            "s.name AS staff_name,",
            "s.id",
            "FROM",
            "staff s",
            "LEFT JOIN dorm_room d ON d.manager_id = s.id" ,
            "<if test='roomIds != null and roomIds !=&apos;&apos;'>",
            " AND d.id IN (${roomIds})",
            "</if>",
            "WHERE",
            "1 = 1",
            "AND s.status = 1",
            "AND s.deleted = 0",
            "AND d.id IS NULL",
            "</script>"})
    public List<DormRoom> getDormRoomManagerList(@Param("roomIds") String roomIds);

    @Select({"<script>",
            "SELECT",
            "s.name AS staff_name,",
            "s.id",
            "FROM",
            "staff s",
            "LEFT JOIN dorm_room d ON d.manager_id = s.id",
            "WHERE",
            "s.status = 1",
            "AND s.deleted = 0",
            "AND ( d.id IS NULL OR d.id = ( SELECT id FROM dorm_room WHERE id = ${id} ))",
            "</script>"})
    public List<DormRoom> getDormRoomManagerListById(@Param("id") String id);

}

